package com.ant.temple.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class AntserfaceView extends SurfaceView implements SurfaceHolder.Callback{

	private SurfaceHolder holder;
	private boolean isRunning=false;
	private Thread thread;

	public AntserfaceView(Context context) {
		super(context);
	}

	public AntserfaceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public AntserfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);

		holder = getHolder();// surfaceview的控制器
		holder.addCallback(this);
		thread = new Thread(new myThread());
	}
	// surfaceview 的生命周期，
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// 创建surface 相当一mvc中的M 里面展示的是数据
		isRunning=true;
		thread.start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		/** 当surface改变时调用的方法
		 * format surface的新的格式，width 新的宽度，height：新的高度
		 */

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// 当surface销毁是调用的方法
		isRunning=false;
	}

	class myThread implements Runnable{
		private float radius=1;
		private Paint paint;


		@Override
		public void run() {
			paint = new Paint();
			paint.setColor(Color.RED);
			paint.setStrokeMiter(2f);
			paint.setStyle(Paint.Style.STROKE);

			while(isRunning){	
				if(radius>100f)
					radius=1f;
				Canvas cavas = holder.lockCanvas();  // 锁定画布
				cavas.drawCircle(200, 200, radius++, paint);
				holder.unlockCanvasAndPost(cavas);   // 解锁画布
			}
		}

	}



}
